package cn.hdu.liang.common.web.session;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;

/**
 * 本地Session
 */
public class HttpSessionProvider implements SessionProvider {
    @Override
    public void setAttribute(HttpServletRequest req, String name, Object value) {
        HttpSession session = req.getSession();
        session.setAttribute(name, value);
    }

    @Override
    public Serializable getAttribute(HttpServletRequest req, String name) {
        HttpSession session = req.getSession(false);
        if(session != null) {
            return (Serializable) session.getAttribute(name);
        }
        return null;
    }

    @Override
    public void logout(HttpServletRequest req) {
        HttpSession session = req.getSession(false);
        if(session != null) {
            session.invalidate();
        }
    }

    @Override
    public String getSessionId(HttpServletRequest req) {
        return req.getSession().getId();
    }
}
